<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="mini" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="仓库编号" prop="wareNo">
        <el-input v-model="queryParams.wareNo" placeholder="请输入" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="仓库名称" prop="wareName">
        <el-input v-model="queryParams.wareName" placeholder="请输入" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="仓库状态" prop="wareStatus">
        <el-select v-model="queryParams.wareStatus" placeholder="请选择" clearable>
          <el-option v-for="dict in dict.type.erp_ware_status" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="负责人">
        <el-input v-model="queryParams.params.nickName" placeholder="请输入" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['erp:ErpWareHouse:add']">新增仓库</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="info" plain icon="el-icon-sort" size="mini" @click="toggleExpandAll">展开/折叠</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-if="refreshTable" v-loading="loading" :data="ErpWareHouseList" row-key="wareId"
      :default-expand-all="isExpandAll" :tree-props="{ children: 'children', hasChildren: 'hasChildren' }" border>
      <el-table-column label="仓库名称" prop="wareName" width="200" />
      <el-table-column label="仓库编号" align="center" prop="wareNo" />
      <el-table-column label="存储类型" align="center" prop="wareTypes">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.erp_ware_types" :value="scope.row.wareTypes" />
        </template>
      </el-table-column>
      <el-table-column label="仓库状态" align="center" prop="wareStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.erp_ware_status" :value="scope.row.wareStatus" />
        </template>
      </el-table-column>
      <el-table-column label="租金" align="center" prop="wareRant">
        <template slot-scope="scope">
          <span>{{ formattedNumber(scope.row.wareRant) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="租期/月" align="center" prop="wareLease" />
      <el-table-column label="负责人" align="center" prop="nickName" />
      <el-table-column label="人员工资" align="center" prop="wareStaffSalary">
        <template slot-scope="scope">
          <span>{{ formattedNumber(scope.row.wareStaffSalary) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="库存数量" align="center" prop="commodityNumber" >
        <template slot-scope="scope">
          <el-link type="primary" @click="handleSelectCommodityNumber(scope.row)">{{scope.row.commodityNumber}}</el-link>
        </template>
      </el-table-column>
      <el-table-column label="仓库地址" prop="wareAddress" width="300"/>
      <el-table-column label="立仓时间" align="center" prop="createTime" />
      <el-table-column label="操作人" align="center" prop="createName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text"  @click="handleUpdate(scope.row)"
            v-hasPermi="['erp:ErpWareHouse:edit']">修改</el-button>
          <el-button size="mini" type="text"  @click="handleUpdate(scope.row)"
            v-hasPermi="['erp:ErpWareHouse:edit']">停用</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改仓库管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="上级仓库" prop="warePid">
              <treeselect v-model="form.warePid" :options="ErpWareHouseOptions" :normalizer="normalizer"
                placeholder="请选择" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="仓库名称" prop="wareName">
              <el-input v-model="form.wareName" placeholder="请输入" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="存储类型" prop="wareTypes">
              <el-select v-model="form.wareTypes" placeholder="请选择">
                <el-option v-for="dict in dict.type.erp_ware_types" :key="dict.value" :label="dict.label"
                  :value="parseInt(dict.value)"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="仓库租金" prop="wareRant">
              <el-input v-model="form.wareRant" placeholder="请输入" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="仓库租期" prop="wareLease">
              <el-input v-model="form.wareLease" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="负责人" prop="userId">
              <el-select filterable v-model="form.userId" placeholder="请选择">
                <el-option v-for="item in userOptions" :key="item.userId" :label="item.nickName"
                  :value="item.userId"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="人员工资" prop="wareStaffSalary">
              <el-input v-model="form.wareStaffSalary" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="仓库状态" prop="wareStatus">
              <el-radio-group v-model="form.wareStatus">
                <el-radio v-for="dict in dict.type.erp_ware_status" :key="dict.value" :label="parseInt(dict.value)">{{
                  dict.label }}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="仓库地址" prop="wareAddress">
              <el-input v-model="form.wareAddress" placeholder="请输入" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


     <!-- 库存商品 -->
     <el-dialog :title="stockTitle" :visible.sync="stockOpen" width="80%" append-to-body :destroy-on-close="true">
      <ErpCommodity v-if="stockOpen"
        :wareId="wareId"></ErpCommodity>
    </el-dialog>
  </div>
</template>

<script>
import { listErpWareHouse, getErpWareHouse, delErpWareHouse, addErpWareHouse, updateErpWareHouse,getCommodityNumberList } from "@/api/erp/ErpWareHouse";
import { getSysUserList } from '@/api/system/user'
import Treeselect from "@riophae/vue-treeselect";
import ErpCommodity from '@/views/erp/ErpCommodity/selectedCommodity'
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import {formattedNumber} from "../../../utils/ruoyi";

export default {
  name: "ErpWareHouse",
  dicts: ['erp_ware_types', 'erp_ware_status'],
  components: {
    Treeselect,
    ErpCommodity
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 仓库管理表格数据
      ErpWareHouseList: [],
      // 仓库管理树选项
      ErpWareHouseOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否展开，默认全部展开
      isExpandAll: true,
      // 重新渲染表格状态
      refreshTable: true,
      // 查询参数
      queryParams: {
        wareName: null,
        wareNo: null,
        wareTypes: null,
        wareStatus: null,
        params: {
          nickName: null
        }
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        warePid: [
          { required: true, message: "上级仓库不能为空", trigger: "blur" }
        ],
        wareName: [
          { required: true, message: "仓库名称不能为空", trigger: "blur" }
        ],
        wareTypes: [
          { required: true, message: "存储类型不能为空", trigger: "blur" }
        ],
        wareRant: [
          { required: true, message: "仓库租金不能为空", trigger: "blur" }
        ],
        wareLease: [
          { required: true, message: "仓库租期不能为空", trigger: "blur" }
        ],
        userId: [
          { required: true, message: "负责人不能为空", trigger: "blur" }
        ],
        wareStatus: [
          { required: true, message: "仓库状态不能为空", trigger: "blur" }
        ],
      },
      // 采购人列表
      userOptions: [],
      // 库存标题
      stockTitle: '',
      stockOpen: false,
      stockList: [],
      wareId: null
    };
  },
  created() {
    this.getList();
    this.handleGetListSysUser();
  },
  methods: {
    formattedNumber,
    /** 查询仓库管理列表 */
    getList() {
      this.loading = true;
      listErpWareHouse(this.queryParams).then(response => {
        this.ErpWareHouseList = this.handleTree(response.data, "wareId", "warePid");
        this.loading = false;
      });
    },
    // 获取用户列表
    handleGetListSysUser() {
      getSysUserList().then(res => {
        this.userOptions = res.data;
      })
    },
    /** 转换仓库管理数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.wareId,
        label: node.wareName,
        children: node.children
      };
    },
    /** 查询仓库管理下拉树结构 */
    getTreeselect() {
      listErpWareHouse().then(response => {
        this.ErpWareHouseOptions = [];
        const data = { wareId: 0, wareName: '顶级节点', children: [] };
        data.children = this.handleTree(response.data, "wareId", "warePid");
        this.ErpWareHouseOptions.push(data);
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        wareId: null,
        wareName: null,
        warePid: null,
        wareNo: 'CK' + this.parseTime(new Date(), '{yyyy}{mm}{dd}{h}{s}{m}'),
        wareTypes: null,
        wareRant: null,
        wareLease: null,
        wareLeve: null,
        userId: null,
        userPhone: null,
        wareStaffSalary: null,
        wareStatus: 1,
        wareAddress: null,
        remark: null,
        identifier: null,
        wareType: null,
        projectId: null,
        staffId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.params.nickName = null;
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset();
      this.getTreeselect();
      if (row != null && row.wareId) {
        this.form.warePid = row.wareId;
      } else {
        this.form.warePid = 0;
      }
      this.open = true;
      this.title = "添加仓库管理";
    },
    /** 展开/折叠操作 */
    toggleExpandAll() {
      this.refreshTable = false;
      this.isExpandAll = !this.isExpandAll;
      this.$nextTick(() => {
        this.refreshTable = true;
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.getTreeselect();
      if (row != null) {
        this.form.warePid = row.wareId;
      }
      getErpWareHouse(row.wareId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改仓库管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.wareId != null) {
            updateErpWareHouse(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addErpWareHouse(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal.confirm('是否确认删除仓库管理编号为"' + row.wareId + '"的数据项？').then(function () {
        return delErpWareHouse(row.wareId);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    // 查询仓库库存商品列表
    handleSelectCommodityNumber(row){
      this.wareId = row.wareId;
      this.stockOpen = true;
      this.stockTitle = row.wareName + '-库存商品列表'
    }
  }
};
</script>
